import 'dart:math';

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // 启动要显示的界面
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int _progress = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("列表滚动测试"),
      ),
      body: NotificationListener(
        onNotification: (ScrollNotification notification ) {
          if (notification is ScrollStartNotification) {
            print("----开始滚动----");
          } else if (notification is ScrollUpdateNotification) {
            // 当前滚动的位置和总长度
            final currentPixel = notification.metrics.pixels;
            final totalPixel = notification.metrics.maxScrollExtent;
            double progress = currentPixel / totalPixel;
            setState(() {
              _progress = (progress * 100).toInt();
            });
            print("正在滚动：${notification.metrics.pixels} - ${notification.metrics.maxScrollExtent}");
          } else if (notification is ScrollEndNotification) {
            print("----结束滚动----");
          }
          return true;
        },
        child: Stack(
          alignment: Alignment(0.9, 0.9),
          children: <Widget>[
            ListView.builder(
                itemCount: 100,
                itemExtent: 60,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(title: Text("item$index"));
                }
            ),
            CircleAvatar(
              radius: 30,
              child: Text("$_progress%"),
              backgroundColor: Colors.black54,
            )
          ],
        ),
      ),
    );
  }
}